const Router = require('@koa/router')
const rewire = require('rewire')
const moment = require('moment')
const {success, fail, routeFn} = rewire('../commonApi.cjs')
const udi = rewire('../common/udi.cjs')

// 有哪些接口呢？
const makeRoute = () => {
  const router = new Router()

  router.post('/udi/test', routeFn(async (ctx, next) => {
    const rs = udi.test(ctx)
    return rs
  }))


  router.post('/udi/list', routeFn(async (ctx, next) => {
    const allow = checkSign(ctx)
    if (allow) {
      const page = getPage(ctx)
      const date = getDate(ctx)
      const size = 20
      return await udi.list(date, page, size)
    }
    return fail(capi.signError, '签名错误')
  }))

  router.post('/udi/detail', async (ctx, next) => {
    const allow = checkSign(ctx)
    if (allow) {
      const id = getId(ctx)
      return udi.detail(id)
    }
    return fail(capi.signError, '签名错误')
  })

  router.post('/udi/save', async (ctx, next) => {
    const allow = checkSign(ctx)
    if (allow) {
      const params = getParams(ctx)
      return udi.save(params)
    }
    return fail(capi.signError, '签名错误')
  })

  return router
}

function checkSign(ctx) {
  // TODO
  console.log('checkSign todo...')
  return true
}

function getPage(ctx) {
  // TODO
  console.log('checkSign todo...')
  return 1
}

function getDate(ctx) {
  // TODO
  console.log('checkSign todo...')
  return '20220102'
}

function getParams(ctx) {
  // TODO
  console.log('getParams todo...')
  return {}
}



module.exports = {makeRoute}
